package cn.mgm.senior.dao.impl;

import cn.mgm.senior.dao.BaseDAO;
import cn.mgm.senior.dao.SysRoleDao;
import cn.mgm.senior.pojo.SysRole;

import java.sql.SQLException;
import java.util.List;

public class SysRoleDaoImpl extends BaseDAO implements SysRoleDao {
    @Override
    public List<SysRole> selectAll() {
        String  sql="SELECT id as Id,role_name as roleName,role_code as roleCode,description as Description FROM sys_role";
        try {
            return this.executeQuery(SysRole.class,sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public SysRole selectById(Integer id) {
        String  sql="SELECT id as Id,role_name as roleName,role_code as roleCode,description as Description FROM sys_role where id=?";
        try {
            return executeQueryBean(SysRole.class,sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(SysRole sysRole) {
        String sql="INSERT INTO sys_role(role_name, role_code, description) VALUES (?,?,?)";
        try {
            return updateExecute(sql,sysRole.getRoleName(),sysRole.getRoleCode(),sysRole.getDescription());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int update(SysRole sysRole) {
        String sql="UPDATE sys_role SET role_name=? where id=?";
        try {
            return updateExecute(sql,sysRole.getRoleName(),sysRole.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delete(Integer id) {
        String sql="DELETE FROM sys_role where id=?";
        try {
            return updateExecute(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
